package model

import (
	"admin_srv/basic/config"
	"time"
)

// 两表联查
type ExpiringVipDetail struct {
	UserId   uint      `json:"user_id"`
	Phone    string    `json:"phone"`
	Nickname string    `json:"nickname"`
	PlanName string    `json:"planName"`
	EndTime  time.Time `json:"endTime"`
}

// 获取即将到期的会员列表
func (end *ExpiringVipDetail) GetExpiringVips(days int) ([]ExpiringVipDetail, error) {
	var details []ExpiringVipDetail
	targetTime := time.Now().AddDate(0, 0, days)
	config.DB.Table("user_vip uv").
		Select("uv.user_id,u.phone,u.nickname,vp.plan_name,uv.end_time").
		Joins("left join user u on uv.user_id = u.id").
		Joins("left join vip_plans vp on uv.plan_id = vp.id").
		Where("uv.is_active = ? and uv.end_time between ? and ?", 1, time.Now(), targetTime).
		Find(&details)

	return details, nil
}
